/************************************************************************
 * <br>
 * <ul>
 * <li>版权所有：唐山弘川科技有限公司 2009－2020</li>
 * <li>创建日期：2009-8-4</li>
 * <li>初始作者：刘川</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 ***********************************************************************/
package com.hy.enterprise.framework.util ;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//import org.jboss.seam.Component;
//import org.jboss.seam.log.LogProvider;
//import org.jboss.seam.log.Logging;

import com.hy.enterprise.framework.util.lang.AnnotationUtil;
import com.hy.enterprise.framework.util.lang.GCUtil;
import com.hy.enterprise.framework.util.logger.SeamLoggerUtil;
//import com.hy.enterprise.framework.lang.annotation.ImplementsBy;
//import com.hy.enterprise.framework.lang.hierarchy.MemoryTreeNode;
import com.hy.enterprise.framework.service.IServiceComponentInvoker;
import com.hy.framework.lang.annotation.ImplementsBy;
//import com.hy.enterprise.framework.widget.api.tree.ITreeBusinessBean;
//import com.hy.enterprise.framework.widget.seam.tree.TreeBusinessBean;

/**
 * <ul>
 * <li>设计作者：刘川</li>
 * <li>设计日期：2009-8-4</li>
 * <li>设计时间：下午01:09:07</li>
 * <li>设计目的：Libra二次开发平台助手类</li>
 * </ul>
 * <ul>
 * <b>修订历史</b>
 * <li>1、</li>
 * </ul>
 */
public class LibraUtil
{
	/**
	 * 日志记录器
	 */
	private static Log logger = LogFactory.getLog(LibraUtil.class);

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-24</li>
	 * <li>设计时间：下午01:40:55</li>
	 * <li>设计目的：将元素类型为：
	 * {@link com.hy.enterprise.framework.lang.hierarchy.MemoryTreeNode}
	 * 的列表转换为元素类型为：
	 * {@link com.hy.enterprise.framework.widget.api.tree.ITreeBusinessBean}
	 * 类型的列表</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param memoryTreeNodes
	 * @return
	 */
	public static List<ITreeBusinessBean> convertMemoryTreeNodesListToTreeBusinessBeansList(
			List<MemoryTreeNode> memoryTreeNodes) {
		List<ITreeBusinessBean> result = null;
		if ((null == memoryTreeNodes) || (memoryTreeNodes.size() == 0)) {
			return result;
		}
		Map<Long, Map<String, MemoryTreeNode>> globalMemoryTreeNodesMap = new HashMap<Long, Map<String, MemoryTreeNode>>();

		result = new ArrayList<ITreeBusinessBean>();
		for (MemoryTreeNode memoryTreeNode : memoryTreeNodes) {
			if (null == memoryTreeNode.getTreeNodeId()) {
				// 树节点不存在标识符，由于当前的所有节点均查询自数据持久，因此如果所获取的节点不存在标识符，
				// 则说明该节点一定为不合法的节点，因此，跳过当前循环，直接执行下一循环
				continue;
			}
			if (null == globalMemoryTreeNodesMap.get(memoryTreeNode
					.getTreeNodeDepth())) {
				Map<String, MemoryTreeNode> temp = new HashMap<String, MemoryTreeNode>();
				globalMemoryTreeNodesMap.put(memoryTreeNode.getTreeNodeDepth(),
						temp);
				GCUtil.nullify(temp);
			}
			globalMemoryTreeNodesMap.get(memoryTreeNode.getTreeNodeDepth())
					.put(memoryTreeNode.getTreeNodeId(), memoryTreeNode);
		}
		if ((null == globalMemoryTreeNodesMap)
				|| (globalMemoryTreeNodesMap.size() == 0)) {
			GCUtil.nullify(result);
			return result;
		}
		Long minLevel = Collections.min(globalMemoryTreeNodesMap.keySet());
		Long maxLevel = Collections.max(globalMemoryTreeNodesMap.keySet());
		Map<String, MemoryTreeNode> currentLevelMemoryTreeNodesMap = null;
		Map<String, MemoryTreeNode> tempLevelMemoryTreeNodesMap = null;
		Map<String, ITreeBusinessBean> currentLevelTreeBusinessBeansMap = new HashMap<String, ITreeBusinessBean>();
		Map<String, ITreeBusinessBean> tempLevelTreeBusinessBeansMap = null;
		for (Long level = maxLevel; level <= minLevel; level--) {
			tempLevelTreeBusinessBeansMap = currentLevelTreeBusinessBeansMap;
			tempLevelMemoryTreeNodesMap = currentLevelMemoryTreeNodesMap;
			currentLevelMemoryTreeNodesMap = globalMemoryTreeNodesMap
					.get(level);
			for (MemoryTreeNode memoryTreeNode : currentLevelMemoryTreeNodesMap
					.values()) {
				currentLevelTreeBusinessBeansMap
						.put(memoryTreeNode.getTreeNodeId(),
								LibraUtil
										.convertMemoryTreeNodeToTreeBusinessBean(memoryTreeNode));
				GCUtil.nullify(memoryTreeNode);
			}
			if ((null != tempLevelTreeBusinessBeansMap)
					&& (tempLevelTreeBusinessBeansMap.size() > 0)) {
				for (MemoryTreeNode memoryTreeNode : tempLevelMemoryTreeNodesMap
						.values()) {
					if (null == currentLevelTreeBusinessBeansMap.get(
							memoryTreeNode.getTreeNodeParentId())
							.getImmediatedNodes()) {
						List<ITreeBusinessBean> immediatedNodes = new ArrayList<ITreeBusinessBean>();
						currentLevelTreeBusinessBeansMap.get(
								memoryTreeNode.getTreeNodeParentId())
								.setImmediatedNodes(immediatedNodes);
						GCUtil.nullify(immediatedNodes);
					}
					currentLevelTreeBusinessBeansMap
							.get(memoryTreeNode.getTreeNodeParentId())
							.getImmediatedNodes()
							.add(tempLevelTreeBusinessBeansMap
									.get(memoryTreeNode.getTreeNodeId()));
					GCUtil.nullify(memoryTreeNode);
				}
			}
		}
		result.addAll(currentLevelTreeBusinessBeansMap.values());
		GCUtil.nullify(currentLevelTreeBusinessBeansMap);
		GCUtil.nullify(currentLevelMemoryTreeNodesMap);
		GCUtil.nullify(tempLevelMemoryTreeNodesMap);
		GCUtil.nullify(tempLevelTreeBusinessBeansMap);
		return result;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-24</li>
	 * <li>设计时间：下午12:05:32</li>
	 * <li>设计目的：将内存树节点转换为树部件所能够处理的树节点业务Bean</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param memoryTreeNode
	 * @return
	 */
	public static ITreeBusinessBean convertMemoryTreeNodeToTreeBusinessBean(
			MemoryTreeNode memoryTreeNode) {
		if (null == memoryTreeNode) {
			return null;
		} else {
			ITreeBusinessBean treeBusinessBean = new TreeBusinessBean();
			treeBusinessBean.setIdentifier(memoryTreeNode.getTreeNodeId());
			treeBusinessBean.setImmediatedNodes(null);
			treeBusinessBean.setIsLeaf(memoryTreeNode.getTreeNodeIsLeaf());
			return treeBusinessBean;
		}
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-4</li>
	 * <li>设计时间：下午12:54:41</li>
	 * <li>设计目的：将数组中为<code>null</code>的元素转换为{@link Object}的类类型</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param argumentsType
	 * @return
	 */
	public static Class < ? > [ ] convertNullToObjectClass (
	                                                         Class < ? > [ ] argumentsType )
	{
		if ( null == argumentsType )
		{
			return null ;
		}

		Class < ? > [ ] result =
		                         new Class [ argumentsType.length ] ;
		for ( int index =
		                  0 ; index < argumentsType.length ; index ++ )
		{
			result [ index ] =
			                   argumentsType [ index ] != null
			                                                  ? argumentsType [ index ]
			                                                  : Object.class ;
		}

		return result ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-4</li>
	 * <li>设计时间：下午01:10:12</li>
	 * <li>设计目的：获取给定对象数组的运行时类型数组</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param args
	 * @return
	 */
	public static Class < ? > [ ] getArgumentTypes (
	                                                 Object [ ] args )
	{
		if ( args == null )
		{
			return null ;
		}

		Class < ? > [ ] types =
		                        new Class [ args.length ] ;
		for ( int index =
		                  0 ; index < args.length ; index ++ )
		{
			types [ index ] =
			                  null != args [ index ]
			                                        ? args [ index ]
			                                                        .getClass ( )
			                                        : null ;
		}

		return types ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-5</li>
	 * <li>设计时间：上午11:54:51</li>
	 * <li>设计目的：获取接口实现类类型的助手方法</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param interfaceClass
	 * @return
	 */
	public static Class<?> getImplementClass(Class<?> interfaceClass) {
		Class<?> clazz = null;
		if (null == interfaceClass) {
			clazz = null;
		} else if (!interfaceClass.isInterface()) {
			clazz = interfaceClass;
		} else {
			if (AnnotationUtil.isClassAnnotationPresent(interfaceClass,
					ImplementsBy.class)) {
				clazz = AnnotationUtil.getAnnotation(interfaceClass,
						ImplementsBy.class).value();
			} else {
				clazz = null;
			}
		}
		if (null == interfaceClass) {
			SeamLoggerUtil.debug(LibraUtil.logger,
					"在LibraUtil.getImplementClass()方法所接收到的参数为NULL");
		} else {
			// SeamLoggerUtil
			// .debug (
			// LibraUtil.logger ,
			// "在LibraUtil.getImplementClass()方法中，将接口：["
			// + interfaceClass
			// + "]类型转换成了其实现类"
			// + clazz ) ;
		}
		return clazz;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-7</li>
	 * <li>设计时间：下午01:00:44</li>
	 * <li>设计目的：根据指定服务名方法名称执行该方法所需要调用的后台服务方法</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param serviceComponentName
	 * @param methodName
	 * @param arguments
	 * @return
	 */
	public static Object invokeSeamService(String serviceComponentName,
			String methodName, Object[] arguments) {
		if ((null == serviceComponentName)
				|| (serviceComponentName.trim().length() == 0)
				|| (methodName == null) || (methodName.trim().length() == 0)) {
			SeamLoggerUtil.debug(LibraUtil.logger, "不存在服务组件或服务方法，返回空。");
			return null;
		} else {
			IServiceComponentInvoker invoker = (IServiceComponentInvoker) Component
					.getInstance(LibraUtil
							.getImplementClass(IServiceComponentInvoker.class));
			return invoker.callComponentMethod(serviceComponentName,
					methodName, arguments);
		}
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-24</li>
	 * <li>设计时间：下午03:44:06</li>
	 * <li>设计目的：</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @return
	 */
	public static Map < String , Object > makeNamedParameter ( )
	{
		return new HashMap < String , Object > ( ) ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-14</li>
	 * <li>设计时间：下午02:50:43</li>
	 * <li>设计目的：创建包含单独命名查询参数的{@link java.util.Map<String, ?>}对象实例</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param parameterName
	 * @param parameterValue
	 * @return
	 */
	public static Map < String , Object > makeNamedParameter (
	                                                           String parameterName ,
	                                                           Object parameterValue )
	{
		Map < String , Object > parameters =
		                                     new HashMap < String , Object > ( ) ;
		parameters
		          .put (
		                 parameterName ,
		                 parameterValue ) ;
		return parameters ;
	}

	/**
	 * 构造函数
	 */
	private LibraUtil ( )
	{
		super ( ) ;
	}

	/**
	 * <ul>
	 * <li>设计作者：刘川</li>
	 * <li>设计日期：2009-8-14</li>
	 * <li>设计时间：下午02:54:03</li>
	 * <li>设计目的：向指定参数{@link java.util.Map<String, Object>}中置入新的参数</li>
	 * </ul>
	 * <ul>
	 * <b>修订历史</b>
	 * <li>1、</li>
	 * </ul>
	 * 
	 * @param parameters
	 * @param parameterName
	 * @param parameterValue
	 * @return
	 */
	protected Map < String , Object > putNamedParameterToMap (
	                                                           Map < String , Object > parameters ,
	                                                           String parameterName ,
	                                                           Object parameterValue )
	{
		if ( null == parameters )
		{
			return LibraUtil
			                .makeNamedParameter (
			                                      parameterName ,
			                                      parameterValue ) ;
		}
		else
		{
			parameters
			          .put (
			                 parameterName ,
			                 parameterValue ) ;
			return parameters ;
		}
	}
}
